/**
 * 玩家
 */
class ZJHPlayerModule extends base.BasePlayerModule 
{
    public isOperating: boolean;//是否正在操作
    public static response: any = {
        ready: "ready",//玩家准备
        startorStopClock: "startorStopClock",
        onGetLookCard: CMDConfig.GET_LOOKCARDS,
        onRoundOver: CMDConfig.GET_ROUNDOVER,
        onCompareCard: CMDConfig.GET_COMPARECARD,//收到比牌
        onQipai: CMDConfig.GET_QIPAI,
        setOperateUI: "setOperateUI",//设置操作UI
        setOperateOverUI: "setOperateOverUI",//设置操作结束
    }

    public static request: any = {
        onTimeRing: "onTimeRing",
        onClockStop: "onClockStop",
    }
    //准备
    public ready(): void
    {
         console.log(">>> ZJHPlayerModule ready 准备");
        if (this.isMyself()) {
            egret.Tween.get(this).wait(3000).call(() => {
                AppData.getInstance().gameData.forbidActionTime = 0;
                if (AppData.getInstance().userData.status == UserStatus.SIT) {//如果是坐下状态，才可准备
                    this.hallSocket.send(CMDConfig.SEND_READY);
                }
            }, this);
        }
    }
    //设置玩家第一次坐下
    public setFirstSitUI(): void 
    {
        console.log(">>> ZJHPlayerModule setFirstSitUI 设置玩家第一次坐下");
        this.callUIByData("setFirstSitUI");
        how.SoundManager.playEffect("zjh_sit_mp3");      
        if (this.isMyself()) {
            this.callUI("setMyFirstSitUI");
        }
    }
    //设置玩家游戏中UI
    public setGameUI(): void 
    {
        console.log(">>> ZJHPlayerModule setGameUI 设置玩家游戏中UI");
        this.callUIByData("setGameUI");
    }
    //设置断线UI
    public setOffLineUI(): void 
    {
        console.log(">>> ZJHPlayerModule setOffLineUI 设置断线UI");
        this.callUIByData("setOffLineUI");
    }
    //设置离开UI
    public setLeaveUI(): void 
    {
        console.log(">>> ZJHPlayerModule setLeaveUI 设置离开UI");
        this.callUIByData("setLeaveUI");
    }
    //设置玩家坐下
    public setSitUI(): void 
    {
        console.log(">>> ZJHPlayerModule setSitUI 设置玩家坐下");
        this.callUIByData("setSitUI");
    }
    //当游戏开始
    public onGameStart(data: any): void
    {
        console.log(">>> ZJHPlayerModule onGameStart 当游戏开始");
        if (this.data) {
            this.callData("setStartData", data);
            this.callUIByData("onGameStart");
            this.callData("setMoneyData", data.lCellScore);
            this.callUI("setMoneyUI", this.data);
            this.callUI("setOperateOverUI", data.lCellScore);
        }
        if (this.data) {//如果自己是庄家
            this.callUI("setBankUI", data.wCurrentUser == this.data.chairID);//需求是先手，所以用wCurrentUser，不用wBankerUser
        }
    }
    //设置操作UI
    public setOperateUI(data: any): void 
    {
        console.log(">>> ZJHPlayerModule setOperateUI 设置操作UI");
        if (this.data && data.wCurrentUser == this.data.chairID) {
            if (this.isMyself()) {
                this.communicate(ZJHOperatePanelModule.response.setOperateUIOperatePanel, AppData.getInstance().gameData);
            }
            if (!data.hasOwnProperty("cbPlayStatus")) {
                this.callUI("setOperateUI", 0);
            }
            else {
                this.callUI("setOperateUI", data.cbPlayStatus[this.data.chairID]);
            }
        }
    }
    public onQipai(data: any): void 
    {
        console.log(">>> ZJHPlayerModule onQipai 弃牌");
        if (this.data && data.wGiveUpUser == this.data.chairID) {
            this.data.status = UserStatus.QIPAI;
            how.SoundManager.playEffect("zjh_giveup_"+ this.data.gender + "_mp3");
        }
    }
    public setOperateOverUI(data: any): void
     {
        console.log(">>> ZJHPlayerModule setOperateOverUI 操作结束");
        if (this.data && data.wAddScoreUser == this.data.chairID) {
            this.callData("setMoneyData", data.lAddScoreCount);
            this.callUI("setMoneyUI", this.data);
            this.callUI("setOperateOverUI", data.lAddScoreCount);
            var gameData: ZJHGameData = <ZJHGameData>AppData.getInstance().gameData;
            if(data.wCompareState == 1)//比牌
            {
                 how.SoundManager.playEffect("zjh_cmp_"+ this.data.gender + "_mp3");
            }else if(gameData.addState == 1)//加注
            {
                 how.SoundManager.playEffect("zjh_add_"+ this.data.gender + "_mp3");
            }else//跟注
            {
                 how.SoundManager.playEffect("zjh_follow0_"+ this.data.gender + "_mp3");
            }
        }
    }
    public onRoundOver(data: any): void {
        this.callUI("onRoundOver");
    }
    /*
     * 倒计时提醒
     * */
    public onTimeRing(index: number): void 
    {
        console.log(">>> ZJHPlayerModule onTimeRing 倒计时提醒");
        if (this.isMyself()) {
            if (index == 0) {
                how.SoundManager.playEffect("zjh_operate_mp3");
                how.Application.ring(LanguageConfig.discardTitle, LanguageConfig.discardMsg);
            }
        }
    }
    /*
    * 根据椅子号开始或者停止玩家的打牌进度条
    * */
    public startorStopClock(chairID: number, flag: Boolean): void 
    {
        console.log(">>> ZJHPlayerModule startorStopClock 根据椅子号开始或者停止玩家的打牌进度条");
        if (this.data && chairID == this.data.chairID) {
            if (flag) {
                this.callUI("startClock");
            } else {
                this.callUI("stopClock");
            }
        }
    }
    /*
    * 倒计时结束
    * */
    public onClockStop(): void 
    {
        console.log(">>> ZJHPlayerModule onClockStop 倒计时结束");
        if (this.isMyself()) {
            this.communicate(ZJHOperatePanelModule.response.autoLead);
        }
    }
    /**
     * 收到看牌消息
     */
    public onGetLookCard(data: any): void 
    {
        console.log(">>> ZJHPlayerModule onGetLookCard 收到看牌消息");
        if (this.data && data.wLookCardUser == this.data.chairID) {
            this.callUI("setLookCardUI");
            how.SoundManager.playEffect("zjh_watch_"+ this.data.gender + "_mp3");
        }
    }
    /**
     * 收到比牌消息
     */
    public onCompareCard(data: any): void 
    {
        console.log(">>> ZJHPlayerModule onCompareCard 收到比牌消息");
        if (this.data && data.wCompareUser.indexOf(this.data.chairID) != -1) {//如果当前用户参与了比牌
            this.callUI("setCompareUI", data.wCurrentUser == this.data.chairID, data.wLostUser == this.data.chairID);
        }
    }
    //当游戏结束
    public onGameEnd(data: any): void 
    {
        super.onGameEnd(data);
        if (this.data) {
            console.log(">>> ZJHPlayerModule onGameEnd 当游戏结束",this.data.chairID);
            var score: number = data.lGameScore[this.data.chairID];
            var isLoss: boolean = score < 0;
            if (!isLoss) {
                this.callData("setMoneyData", -score);
                this.callUI("setMoneyUI", this.data);
            }
            this.callUI("setGameEndUI", isLoss, data.cbCardData[this.data.chairID]);
            this.callUI("stopClock");
        }
        this.ready();//自动准备
    }
}